<template>
  <div>
    <van-nav-bar title="账号登录"></van-nav-bar>
    <van-form @submit="OnSubmit">
      <van-field
        v-model="username"
        :rules="[
          { required: true, message: ' 请输入用户名' },
          { pattern: /^\w{6,16}$/, message: '用户名必须是6-16位' },
        ]"
        label="用户名"
        name="username"
        placeholder="用户名"
      ></van-field>
      <van-field
        v-model="password"
        :rules="[
          { required: true, message: ' 请输入密码' },
          { pattern: /^\w{6,16}$/, message: '密码必须是6-16位' },
        ]"
        label="密码"
        name="password"
        placeholder="密码"
        type="password"
      ></van-field>
      <div style="margin: 16px">
        <van-button block type="info">提交</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { login } from '@/api/user'
import { setToken } from '@/utils/storage'

export default {
  name: 'loginIndex',
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async OnSubmit (values) {
      const a = await login(values)
      console.log(a)
      const {
        body: { token }
      } = await login(values)
      // token存到本地
      setToken(token)
      // 提示登陆成功
      this.$toast.success('登录成功')
      // 跳转到首页
      this.$router.push('/')
    }
  }
}
</script>

<style scoped>

</style>
